package com.gongpingjialoan.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.SeeCarActivity;
import com.gongpingjialoan.activity.car.SeeCarFragment;
import com.gongpingjialoan.activity.car.SellCarAddressActivity;
import com.gongpingjialoan.widget.adapters.ArrayWheelAdapter;
import com.gongpingjialoan.widget.views.OnWheelChangedListener;
import com.gongpingjialoan.widget.views.WheelView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2015/9/30.
 */
public class DateTimePickDailog extends Dialog implements View.OnClickListener {

    private Context mContext;

    private int day_num;

    private WheelView yearWheelView, timeWheelView;

    private ArrayWheelAdapter<String> stringArrayWheelAdapter;

    private String[] datas;

    private String[] times;

    private ArrayWheelAdapter<String> numericWheelAdapter;

    private DateFormat hh = new SimpleDateFormat("HH");

    private TextView cancleTextView, confirmTextView;

    private Fragment mFragment;

    private Date[] d;

    boolean isNoToday = false;

    public DateTimePickDailog(Context mContext, int num, Fragment fragment) {
        super(mContext, R.style.dialogs);
        this.mContext = mContext;
        this.day_num = num;
        this.mFragment = fragment;

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_selct);
        initView();
    }



    private String[] getDay() {
        DateFormat df = new SimpleDateFormat("MM月dd日");
        Calendar calendar = Calendar.getInstance();
        String[] days = new String[day_num];
        d = new Date[day_num];
        if (calendar.get(Calendar.HOUR_OF_DAY) >= 22) {
            isNoToday = true;
            calendar.add(Calendar.DATE, 1);
        }
        days[0] = df.format(calendar.getTime());
        d[0] = calendar.getTime();
        for (int i = 1; i < day_num; i++) {
            calendar.add(Calendar.DATE, 1);
            days[i] = df.format(calendar.getTime());
            d[i] = calendar.getTime();
        }
        return days;

    }


    private void initView() {
        yearWheelView = (WheelView) findViewById(R.id.year_view);
        timeWheelView = (WheelView) findViewById(R.id.time_view);
        cancleTextView = (TextView) findViewById(R.id.btn_myinfo_cancel);
        confirmTextView = (TextView) findViewById(R.id.btn_myinfo_sure);
        cancleTextView.setOnClickListener(this);
        confirmTextView.setOnClickListener(this);
//        yearWheelView.setWheelBackground(R.color.white);
//        timeWheelView.setWheelBackground(R.color.white);
        datas = getDay();
        times = getHour(15);
        stringArrayWheelAdapter = new ArrayWheelAdapter<String>(mContext, datas);
        stringArrayWheelAdapter.setItemResource(R.layout.time_tiem);
        stringArrayWheelAdapter.setItemTextResource(R.id.text);
        yearWheelView.setViewAdapter(stringArrayWheelAdapter);

//        numericWheelAdapter =new ArrayWheelAdapter<String>(mContext,times);
//        numericWheelAdapter.setItemResource(R.layout.time_tiem);
//        numericWheelAdapter.setItemTextResource(R.id.text);
//        timeWheelView.setViewAdapter(numericWheelAdapter);

        yearWheelView.setCurrentItem(0);
        yearWheelView.setVisibleItems(3);
        timeWheelView.setCurrentItem(0);
        timeWheelView.setVisibleItems(3);
        yearWheelView.setCyclic(true);
        timeWheelView.setCyclic(true);
        updateHour();
        yearWheelView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                updateHour();
            }
        });

    }


    private String[] getHour(int hours) {
        String[] days = new String[hours + 1];
        for (int i = 0; i <= hours; i++) {
            days[i] = i + 7 + "点";
        }
        return days;
    }


    private void updateHour() {
        int item = yearWheelView.getCurrentItem();
        if (item == 0 && !isNoToday) {
            int hour = Integer.parseInt(hh.format(Calendar.getInstance().getTime()));
            int size = 22 - hour;
            String[] days = new String[size];
            for (int i = 0; i < size; i++) {
                days[i] = String.valueOf(i + hour + 1) + "点";
            }
            times = days;
            numericWheelAdapter = new ArrayWheelAdapter<String>(mContext, times);
            numericWheelAdapter.setItemResource(R.layout.time_tiem);
            numericWheelAdapter.setItemTextResource(R.id.text);
            timeWheelView.setViewAdapter(numericWheelAdapter);
            timeWheelView.setCurrentItem(0);
        } else {
            times = getHour(15);
            numericWheelAdapter = new ArrayWheelAdapter<String>(mContext, times);
            numericWheelAdapter.setItemResource(R.layout.time_tiem);
            numericWheelAdapter.setItemTextResource(R.id.text);
            timeWheelView.setViewAdapter(numericWheelAdapter);
            timeWheelView.setCurrentItem(0);
        }
    }


    @Override
    public void onClick(View view) {
        if (view == cancleTextView) {
            dismiss();
        }
        if (view == confirmTextView) {
            if(mFragment == null){
                String date = datas[yearWheelView.getCurrentItem()];
                String time = times[timeWheelView.getCurrentItem()];
                if(mContext instanceof SeeCarActivity) {
                    SeeCarActivity seeCarActivity = (SeeCarActivity) mContext;
                    seeCarActivity.setTime(date + " " + time, d[yearWheelView.getCurrentItem()], time);
                }else if(mContext instanceof SellCarAddressActivity){
                    SellCarAddressActivity sellCarAddressActivity = (SellCarAddressActivity) mContext;
                    sellCarAddressActivity.setTime(date + " " + time, d[yearWheelView.getCurrentItem()], time);
                }
                dismiss();
            }else {
                SeeCarFragment seeCarFragment = (SeeCarFragment) mFragment;
                String date = datas[yearWheelView.getCurrentItem()];
                String time = times[timeWheelView.getCurrentItem()];
                seeCarFragment.setTime(date + " " + time, d[yearWheelView.getCurrentItem()], time);
                dismiss();
            }
        }
    }


}
